package com.ltkj.ticket.service.impl;

import java.util.List;

import com.ltkj.common.core.exception.ServiceException;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.redis.service.RedisService;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.ticket.domain.TicOtherBase;
import com.ltkj.ticket.domain.dto.AppletTicketQueryDTO;
import com.ltkj.ticket.domain.vo.AppletTicketPageVo;
import com.ltkj.ticket.mapper.AppletTicketMapper;
import com.ltkj.ticket.mapper.TicOtherBaseMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ltkj.ticket.mapper.TicScenicMapper;
import com.ltkj.ticket.domain.TicScenic;
import com.ltkj.ticket.service.ITicScenicService;

/**
 * 景区门票基础Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-31
 */
@Service
public class TicScenicServiceImpl implements ITicScenicService 
{
    private static final Logger log = LoggerFactory.getLogger(TicScenicServiceImpl.class);

    @Autowired
    private TicScenicMapper ticScenicMapper;
    @Autowired
    private TicOtherBaseMapper ticOtherBaseMapper;
    @Autowired
    private AppletTicketMapper appletTicketMapper;
    @Autowired
    private RedisService redisService;


    /**
     * 查询景区门票基础
     * 
     * @param scenicId 景区门票基础主键
     * @return 景区门票基础
     */
    @Override
    public TicScenic selectTicScenicByScenicId(Long scenicId)
    {
        return ticScenicMapper.selectTicScenicByScenicId(scenicId);
    }

    /**
     * 查询景区门票基础列表
     * 
     * @param ticScenic 景区门票基础
     * @return 景区门票基础
     */
    @Override
    public List<TicScenic> selectTicScenicList(TicScenic ticScenic)
    {
        return ticScenicMapper.selectTicScenicList(ticScenic);
    }

    /**
     * 新增景区门票基础
     * 
     * @param ticScenic 景区门票基础
     * @return 结果
     */
    @Override
    public int insertTicScenic(TicScenic ticScenic)
    {
        try {
            ticScenic.setIsDel("0");
            ticScenic.setCreateBy(SecurityUtils.getUsername());
            ticScenic.setCreateTime(DateUtils.getNowDate());
            //根据当前时间判断票的状态
            if(DateUtils.getNowDate().after(ticScenic.getEffectiveTimeStart())&&
                    DateUtils.getNowDate().before(ticScenic.getEffectiveTimeEnd())){
                ticScenic.setScenicStatus("2");
            }
            if (DateUtils.getNowDate().after(ticScenic.getEffectiveTimeEnd())){
                ticScenic.setScenicStatus("3");
            }
            if (DateUtils.getNowDate().before(ticScenic.getEffectiveTimeStart())){
                ticScenic.setScenicStatus("1");
            }
            ticScenicMapper.insertTicScenic(ticScenic);
            //查询小程序列表
            AppletTicketQueryDTO queryDTO = new AppletTicketQueryDTO();
            queryDTO.setTicketType("1");
            List<AppletTicketPageVo> pageList = appletTicketMapper.list(queryDTO);
            //向redis中缓存景区门票列表
            redisService.setCacheList("PAGE:page_tic_scenic", pageList);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("新增景区门票异常");
        }
        return 200;
    }

    /**
     * 修改景区门票基础
     * 
     * @param ticScenic 景区门票基础
     * @return 结果
     */
    @Override
    public int updateTicScenic(TicScenic ticScenic)
    {
        try {
            ticScenic.setUpdateBy(SecurityUtils.getUsername());
            ticScenic.setUpdateTime(DateUtils.getNowDate());
            ticScenicMapper.updateTicScenic(ticScenic);
            //查询小程序列表
            AppletTicketQueryDTO queryDTO = new AppletTicketQueryDTO();
            queryDTO.setTicketType("1");
            List <AppletTicketPageVo> pageList = appletTicketMapper.list(queryDTO);
            //向redis中缓存景区门票列表
            redisService.udpateCacheList("PAGE:page_tic_scenic", pageList);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("修改景区门票异常");
        }
            return 200;
    }

    /**
     * 批量删除景区门票基础
     * 
     * @param scenicIds 需要删除的景区门票基础主键
     * @return 结果
     */
    @Override
    public int deleteTicScenicByScenicIds(Long[] scenicIds)
    {
        try {
            ticScenicMapper.deleteTicScenicByScenicIds(scenicIds);
            //查询小程序列表
            AppletTicketQueryDTO queryDTO = new AppletTicketQueryDTO();
            queryDTO.setTicketType("1");
            List <AppletTicketPageVo> pageList = appletTicketMapper.list(queryDTO);
            //向redis中缓存景区门票列表
            redisService.udpateCacheList("PAGE:page_tic_scenic", pageList);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("修改景区门票异常");
        }
            return 200;
    }

    /**
     * 删除景区门票基础信息
     * 
     * @param scenicId 景区门票基础主键
     * @return 结果
     */
    @Override
    public int deleteTicScenicByScenicId(Long scenicId)
    {
        return ticScenicMapper.deleteTicScenicByScenicId(scenicId);
    }

    @Override
    public AjaxResult selectTicOtherBaseList(String otherBaseType) {
        TicOtherBase otherBase = new TicOtherBase();
        otherBase.setOtherBaseType(otherBaseType);
        otherBase.setIsDel("0");
        List<TicOtherBase> list = ticOtherBaseMapper.selectTicOtherBaseList(otherBase);
        return AjaxResult.success(list);
    }

    @Override
    public int offshelf(Long scenicId) {
        TicScenic ticScenic = ticScenicMapper.selectTicScenicByScenicId(scenicId);
        if ("1".equals(ticScenic.getIsDel())) {
            throw new ServiceException("数据不存在");
        }
        return ticScenicMapper.offshelf(scenicId,SecurityUtils.getUsername());
    }

    @Override
    public int onshelf(Long scenicId) {
        TicScenic ticScenic = ticScenicMapper.selectTicScenicByScenicId(scenicId);
        if ("1".equals(ticScenic.getIsDel())) {
            throw new ServiceException("数据不存在");
        }
        return ticScenicMapper.onshelf(scenicId,SecurityUtils.getUsername());
    }
}
